package com.xhxi.photobooker.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhxi.photobooker.entity.PortfolioLike;
import com.xhxi.photobooker.mapper.PortfolioLikeMapper;
import com.xhxi.photobooker.service.PortfolioLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PortfolioLikeServiceImpl implements PortfolioLikeService {
    @Autowired
    private PortfolioLikeMapper likeMapper;



    public boolean like(Long userId, Long portfolioId) {
        if (hasLiked(userId, portfolioId)) return false;
        PortfolioLike like = new PortfolioLike();
        like.setUserId(userId);
        like.setPortfolioId(portfolioId);
        likeMapper.insert(like);
        return true;
    }

    public boolean unlike(Long userId, Long portfolioId) {
        return likeMapper.delete(
                new QueryWrapper<PortfolioLike>()
                        .eq("user_id", userId)
                        .eq("portfolio_id", portfolioId)
        ) > 0;
    }


    @Override
    public boolean hasLiked(Long userId, Long portfolioId) {
        return likeMapper.countByUserAndPortfolio(userId, portfolioId) > 0;
    }
}